<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['configuration:teamsigningrewards:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['configuration:teamsigningrewards:remove']">批量删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="teamsigningrewardsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="sort" />
      <el-table-column label="消费金额" align="center">
        <template slot-scope="scope">
          {{ scope.row.type == 0 ? (scope.row.price + '及以上') : scope.row.price }}
        </template>
      </el-table-column>
      <el-table-column label="享受折扣" align="center" prop="discount">
        <template slot-scope="scope">
          {{ (scope.row.discount && scope.row.discount != 100) ? ( accMul(scope.row.discount, 0.1) + '折') : '无' }} </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['configuration:teamsigningrewards:edit']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['configuration:teamsigningrewards:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改团队签约奖励对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="消费金额" prop="price">
          <el-row>
            <el-col :span="14">
              <el-input v-model="form.price" placeholder="请输入消费金额" type="number" />
            </el-col>
            <el-col :span="10">
              <el-radio-group v-model="form.type" inline>
                <el-radio-button :label="0">以上</el-radio-button>
                <el-radio-button :label="1">常规</el-radio-button>
              </el-radio-group>
            </el-col>
          </el-row>
        </el-form-item>



        <el-form-item label="折扣" prop="discount">
          <el-input v-model="form.discount" placeholder="请输入折扣" type="number" min="0" max="9.9" @input="handleInput" />
        </el-form-item>

        <el-form-item label="显示排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入序号" type="number" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTeamsigningrewards, getTeamsigningrewards, delTeamsigningrewards, addTeamsigningrewards, updateTeamsigningrewards } from "@/api/configuration/teamsigningrewards";
import { accMul } from "@/utils/math";
export default {
  name: "Teamsigningrewards",
  data() {
    return {
      // 遮罩层
      loading: true,
      radio1: '0',
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 团队签约奖励表格数据
      teamsigningrewardsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        price: null,
        type: null,
        discount: null,
        sort: null,
      },
      // 表单参数
      form: {
        type: 1,
        sort: 1,
        discount: null,
        price: ''
      },
      // 表单校验
      rules: {
        price: [
          { required: true, message: "消费金额不能为空", trigger: "blur" },
          { validator: this.priceValidator, message: '消费金额不能小于0.01', trigger: 'blur' }
        ],
        type: [
          { required: true, message: "预存类型 0  以上  1 常规不能为空", trigger: "change" }
        ],
        discount: [
          { required: true, message: "折扣不能为空", trigger: "blur" },
          { type: "number", validator: this.discountValidator, message: '请输入1-9.9之间的数字', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    accMul,
    handleInput(event) {
      let newValue = event;
      // 移除非数字和小数点之外的字符
      newValue = newValue.replace(/[^\d.]/g, '');
      // 只保留第一个小数点
      newValue = newValue.replace(/^\./, '');
      newValue = newValue.replace(/\.(?=.*\.)/g, '');
      // 限制在0到9.9之间
      if (newValue !== '' && (parseFloat(newValue) < 0 || parseFloat(newValue) > 9.9)) {
        newValue = parseFloat(newValue) < 0 ? '0' : '9.9';
      }
      // 限制小数点后只能输入一位数字
      const decimalIndex = newValue.indexOf('.');
      if (decimalIndex !== -1 && newValue.length - decimalIndex > 2) {
        newValue = newValue.slice(0, decimalIndex + 2);
      };
      this.form.discount = newValue;
    },

    priceValidator(rule, value, callback) {
      if (value < 0.01) {
        return callback(new Error('输入金额最小为0.01!'))
      }
      callback();
    },

    discountValidator(rule, value, callback) {
      if (value < 1 || value > 9.9) {
        return callback(new Error('输入折扣为1 - 9.9的数字'))
      }
      callback();
    },


    /** 查询团队签约奖励列表 */
    getList() {
      this.loading = true;
      listTeamsigningrewards(this.queryParams).then(response => {
        this.teamsigningrewardsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        price: null,
        type: 1,
        discount: null,
        sort: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加团队签约奖励";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTeamsigningrewards(id).then(response => {
        this.form = { ...response.data, discount: this.accMul(response.data.discount, 0.1) };
        this.open = true;
        this.title = "修改团队签约奖励";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTeamsigningrewards({
              ...this.form,
              discount: this.accMul(this.form.discount,10) ,
            }).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTeamsigningrewards({
              ...this.form,
              discount: this.accMul(this.form.discount,10),
            }).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('确认要删除该团队签约奖励吗?').then(function () {
        return delTeamsigningrewards(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('configuration/teamsigningrewards/export', {
        ...this.queryParams,
        ids: this.ids.length ? this.ids : undefined
      }, `teamsigningrewards_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
